package com.godpaper.the5elements.support.graph.searcher
{
	import de.polygonal.ds.Graph;
	import de.polygonal.ds.GraphNode;
	
	import mx.collections.ArrayCollection;
	
	public class GraphSearcher
	{
		protected var graphRef:Graph;
		public function GraphSearcher(graphRef:Graph)
		{
			this.graphRef = graphRef;
		}
		
		private var _startNode:GraphNode;
		public function get startNode():GraphNode
		{
			return _startNode;
		}
		public function set startNode(value:GraphNode):void
		{
			_startNode = value;
		}
		
		private var _endNode:GraphNode;
		public function get endNode():GraphNode
		{
			return _endNode;
		}
		public function set endNode(value:GraphNode):void
		{
			_endNode = value;
		}
		
		public function searchAllSimplePath(graph:Graph,visited:ArrayCollection):void
		{
			var node:GraphNode = graph.nodes[visited.getItemAt(visited.length-1)];
			for(node in graph.nodes)
			{
				if(visited.contains(node))
				{
					continue;
				}
				if(node===endNode)
				{
					visited.addItem(node);
					printPath(visited);
					visited.removeItemAt(visited.length-1);
					break;
				}
			}
			// in depth-first, recursion needs to come after visiting adjacent nodes
			for(node in graph.nodes)
			{
				if(visited.contains(node)||node===endNode)
				{
					continue;
				}
				visited.addItem(node);
				searchAllSimplePath(graph,visited);
				visited.removeItemAt(visited.length-1);
			}
		}
		
		public function printPath(visited:ArrayCollection):void
		{
			for(var node:GraphNode in visited)
			{
				trace(node,"\t");
			}
		}

	}
}